//
//  YCUserDefault.m
//  YouChi
//
//  Created by sam on 15/5/25.
//  Copyright (c) 2015年 Guangzhou QuicklyANT Information Technology Guangzhou QuicklyANT Information Technology Guangzhou QuicklyANT Information Technology Co.,LTD. All rights reserved.
//

#import "MCUserDefault.h"


static NSString *kNowUser  = @"NowUser";

NSString *YCUserDefaultUpdate = @"YCUserDefaultUpdate";
NSString *YCUserDefaultLogin = @"YCUserDefaultLogin";

@interface MCUserDefault()
{
    NSUserDefaults *_userDefaults;
}
@property (nonatomic,strong) UserModel *login;
//@property (nonatomic,strong) NSMutableArray *loginIds;

@end

@implementation MCUserDefault
-(void)dealloc{
    
}

+ (MCUserDefault *)standardUserDefaults {
    static MCUserDefault *_standardUserDefaults = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _standardUserDefaults = [[self alloc]init];
    });
    
    
    return _standardUserDefaults;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        _userDefaults = [NSUserDefaults standardUserDefaults];
        
        NSData *data = [_userDefaults objectForKey:apiLogin];
        id login = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        if (login) {
            _login = login;
        }
        else
        {
            _login = [UserModel new];
        }
        
        
        
    }
    return self;
}

- (UserModel *)loadUser
{
    NSData *data = [_userDefaults objectForKey:apiLogin];
    if (data!=nil) {
        self.login = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        return self.login;
    }
    return nil;

}

- (BOOL)saveUser:(UserModel *)login
{
    [_userDefaults removeObjectForKey:apiLogin];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:login];
    [_userDefaults setObject:data forKey:apiLogin];
    self.login = login;
    return [_userDefaults synchronize];
}

- (BOOL)saveUser
{
    return [self saveUser:self.login];
}

- (void)updateCurrentUser:(UserModel *)login
{
    self.login = login;
    
    //[[NSNotificationCenter defaultCenter]postNotificationName:YCUserDefaultUpdate object:login];
}



+ (UserModel *)currentUser
{
    UserModel *login = [self standardUserDefaults].login;
    if (!login) {
        login = [[self standardUserDefaults] loadUser];
    }
    return login;
}

- (BOOL)removeCurrentUser
{
    
    //[self updateCurrentUser:nil];
    BOOL b = [self saveUser:nil];
    
    //[[NSNotificationCenter defaultCenter]postNotificationName:YCUserDefaultLogin object:nil];
    
    return b;
}


/*
- (BOOL)isCurrentUserValid
{
    MCLoginModel *login = self.login;
    
    YCLoginUserM *loginUser = login.appUser;
    BOOL b = (loginUser.loginId.length >0 && login.token.length >0);
    return b;
}

- (BOOL)isMoneyUserValid
{
    MCLoginModel *login = self.login;
    YCLoginUserM *loginUser = login.appUser;
    BOOL b = (loginUser.loginId.length >0 && login.token.length >0&&login.appUser.balance ==nil &&loginUser.balance ==nil);
    return b;

}
 */



/*

- (BOOL)addCurrentUser:(MCLoginModel *)login forLoginId:(NSString *)loginId
{
    if (loginId) {
        if (![self.loginIds containsObject:loginId]) {
            [self.loginIds addObject:loginId];
            [_userDefaults setObject:self.loginIds forKey:kAllUser];
        }
    }
    
    [self updateCurrentUser:login];
    BOOL b =  [self saveUser:login];
    
    if (b) {
        [[NSNotificationCenter defaultCenter]postNotificationName:YCUserDefaultLogin object:login];
    }
    return b;
}

- (BOOL)addCurrentUser:(MCLoginModel *)login
{
    return [self addCurrentUser:login forLoginId:nil];
}


 
 - (NSString *)loadPasswordForUser:(MCLoginModel *)login
 {
 return [self loadPasswordForLoginId:login.userModel.loginId];
 }
 
 - (NSString *)loadPasswordForLoginId:(NSString *)loginId
 {
 if (![loginId isKindOfClass:[NSString class]]) {
 return nil;
 }
 NSString *password = [SSKeychain passwordForService:kService account:loginId];
 return password;
 }
 
 
 
 - (BOOL )savePassword:(NSString *)psw forUser:(MCLoginModel *)login
 {
 return [self savePassword:psw forLoginId:login.userModel.loginId];
 }
 
 - (BOOL)savePassword:(NSString *)psw forLoginId:(NSString *)loginId
 {
 if (![loginId isKindOfClass:[NSString class]]) {
 return NO;
 }
 
 [SSKeychain setPassword:psw forService:kService account:loginId];
 
 [_userDefaults setObject:loginId forKey:kAllUser];
 return [_userDefaults synchronize];
 }
 
 - (NSArray *)getAccountAndPassword
 {
 NSString *loginId = [_userDefaults objectForKey:kAllUser];
 NSString *password = [SSKeychain passwordForService:kService account:loginId];
 
 if (loginId) {return @[loginId, password];}
 return nil;
 }

 */


+(BOOL)isLogin
{
   NSString *Id = [MCUserDefault currentId].stringValue;
    if (Id.length <= 0 || [Id isEqualToString:@"0"] || !self) {
        return YES;
    }
    return NO;
}


+ (NSString *)currentToken
{
    NSString *token = [MCUserDefault currentUser].token;

    return token?token:@"";

}



+ (NSNumber *)currentId
{
    id Id = [MCUserDefault currentUser].user_id;
    return Id?Id:@0;
}
+ (NSNumber *)currentShopId
{
    id Id = [MCUserDefault currentUser].shop_id;
    return Id?Id:@0;
}
/*

- (NSString *)lastLoginId
{
    return [_userDefaults stringForKey:kLastUser];
}

- (BOOL)setlastLoginId:(NSString *)loginId
{
    [_userDefaults setObject:loginId forKey:kLastUser];
    return [_userDefaults synchronize];
}

- (NSArray *)allLoginIds
{
    return self.loginIds.copy;
}

- (BOOL)removeLoginId:(NSString *)loginId
{
    if ([self.loginIds containsObject:loginId]) {
        [self.loginIds removeObject:loginId];
        [_userDefaults setObject:self.loginIds forKey:kAllUser];
        return [_userDefaults synchronize];
    }
    return NO;
}
 
 */
@end
